<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 车展同步接口数据
 */
class CarSync{
    //车展api请求地址
    var $apiUrl = '';
    //签名密钥
    var $token = '';
    //访问密钥
    var $accessToken = '';
    //接口编码
    var $mainCmdList = array();
    //接口扩展地址
    var $apiExtList = array();
    //提交数据
    var $data = array();
    /**
     * 构造函数
     */
    function CarSync(){
        //接口地址
        if (ENV == 'production'){//生产环境
            $this->apiUrl = 'http://btl.dongfeng-nissan.com.cn/btlcz/';
        }else{
            $this->apiUrl = 'http://e4s.stg.dongfeng-nissan.com.cn/btlcz';
        }
        //token
        $this->token = '3d2ca3e70a2409712318e5b686e4a6f8bf8b9426dc164d09d3eed87e26976b6766d8a7b29bd7aad5c3c90962264770323f2159c46375e703fd17a5646c7cb231';
        //接口编码
        $this->mainCmdList = array(
            'token'     => 'OA00001',
            'sum'       => 'OA00002',
            'project'   => 'OA00003',
            'gx'        => 'OA00004',
            'rent'      => 'OA00006',
            'matching'  => 'OA00007',
        );
        //接口扩展地址
        $this->apiExtList = array(
            'token'     => '/api/token',
            'sum'       => '/api/rmp/sum',
            'project'   => '/api/rmp/projects',
            'gx'        => '/api/rmp/ad/costs',
            'rent'      => '/api/rmp/area/rent',
            'matching'  => '/api/rmp/support/cost',
        );
        //初始化访问权限
        $this->getAccessToken();
    }

    /**
     * Curl
     * @param string $url  APIURL
     * @param josn $data  POST数据
     * @param array $header  header
     * @return boolean
     */
    function httpGet($url, $data = '', $header = array(), $method = 'POST')
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); //设置请求方式
        if ($data) {
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 500);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_URL, $url);
        $res = curl_exec($curl);
        curl_close($curl);
        return $res;
    }
    /**
     * 获取访问权限
     * @return string
     */
    function getAccessToken(){
        if (!$this->accessToken){
            $url = $this->apiUrl . $this->apiExtList['token'];
            $data = array(
                'token'     => $this->token,
                'reqSeq'    => time(),
                'mainCmd'   => $this->mainCmdList['token'],
                'grantType' => 'appClient',
            );
            $returnData = $this->httpGet($url.'?'.httpBuildQuery($data), $data, array(), 'GET');
            $jsonData = jsonDecode($returnData);
            if (isset($jsonData['authcHead']) && $jsonData['authcHead']['retnCode'] == 0){
                $this->accessToken = $jsonData['authcHead']['accessToken'];
            }
            if (!$this->accessToken){
                exit('获取登录访问权限失败');
            }
        }
        return $this->accessToken;
    }
    /**
     * 清除数据
     */
    function clearData(){
        $this->data = array();
    }
    /**
     * 包装汇总数据
     * @param array $data
     */
    function packSumData($sumListData = array()){
        //清除数据
        $this->clearData();
        foreach ($sumListData as $v){
            $this->data[] = array(
                'procId'    => $v['id'],
                'dlrCode'   => $v['dlrCode'],
                'dlrName'   => $v['dlrName'],
                'year'      => $v['year'],
                'month'     => $v['timeUnitStr'],
                'budgetApplyGx'     => $v['budgetApplyGx'],
                'budgetApplyRent'   => $v['budgetApplyRent'],
                'budgetApplyMatching' => $v['budgetApplyMatching'],
                'budgetApply' => $v['budgetApply'],
                'fileUrl'   => $v['fileUrl'],
                'subsidyGx' => $v['subsidyGx'],
                'settleApplyGx' => $v['settleApplyGx'],
                'canSettleGx' => $v['canSettleGx'],
                'settleStoreTotal' => $v['settleStoreTotal'],
                'settleQiTotal' => $v['settleQiTotal'],
                'settleRmpSupport' => $v['settleRmpSupport'],
                'settleApplyRent' => $v['settleApplyRent'],
                'subsidyMatching' => $v['subsidyMatching'],
                'settleApplyMatching' => $v['settleApplyMatching'],
                'canSettleMatching' => $v['canSettleMatching'],
                'settleApply' => $v['settleApply'],
                'kpi' => $v['kpi'],
                'finalSettleMoney' => $v['finalSettleMoney'],
            );
        }
        return $this->sendData('sum');
    }
    /**
     * 包装项目数据
     * @param array $projectData
     */
    function packProjectData($projectListData = array()){
        //清除数据
        $this->clearData();
        foreach ($projectListData as $v){
            $this->data[] = array(
                'projectId'    => $v['id'],
                'dlrCode'   => $v['dlrCode'],
                'dlrName'   => $v['dlrName'],
                'procId'    => $v['procId'],
                'year'      => $v['year'],
                'month'     => $v['timeUnitStr'],
                'name'      => $v['name'],
                'levelStr'  => $v['levelStr'],
                'startTime' => $v['startTime'],
                'endTime'   => $v['endTime'],
                'address'   => $v['address'],
                'budgetApplyGx' => $v['budgetApplyGx'],
                'budgetApplyRent' => $v['budgetApplyRent'],
                'budgetApplyMatching' => $v['budgetApplyMatching'],
                'budgetApply' => $v['budgetApply'],
                'budgetAuditStr' => $v['budgetAuditStr'],
                'budgetAudit' => $v['budgetAudit'],
                'settleApplyGx' => $v['settleApplyGx'],
                'settleApplyRent' => $v['settleApplyRent'],
                'settleStoreTotal' => $v['settleStoreTotal'],
                'settleQiTotal' => $v['settleQiTotal'],
                'settleRmpSupport' => $v['settleRmpSupport'],
                'settleApplyMatching' => $v['settleApplyMatching'],
                'subsidyMatching' => $v['subsidyMatching'],
                'settleApply' => $v['settleApply'],
                'settleAuditStr' => $v['settleAuditStr'],
                'settleAudit' => $v['settleAudit'],
                
            );
        }
        return $this->sendData('project');
    }
    /**
     * 包装广宣数据
     * @param array $gxData
     */
    function packGxData($gxListData = array()){
        //清除数据
        $this->clearData();
        foreach ($gxListData as $v){
            $this->data[] = array(
                'serialId'    => $v['id'],
                'dlrCode'   => $v['dlrCode'],
                'dlrName'   => $v['dlrName'],
                'procId'    => $v['procId'],
                'projectId' => $v['projectId'],
                'year'      => $v['year'],
                'month'     => $v['timeUnitStr'],
                'mediaTypeStr' => $v['mediaType1Str'].'<br>'.$v['mediaType2Str'],
                'mediaName' => $v['mediaName'],
                'mediaSizeStr' => $v['mediaSizeStr'],
                'startTime' => $v['startTime'],
                'endTime' => $v['endTime'],
                'frequency' => $v['frequency'],
                'remarks' => $v['remarks'],
                'budgetApply' => $v['budgetApply'],
                'budgetAuditStr' => $v['budgetAuditStr'],
                'budgetAudit' => $v['budgetAudit'],
                'settleFileUrl' => $v['fileUrl'],
                'settleApply' => $v['settleApply'],
                'settleAuditStr' => $v['settleAuditStr'],
                'settleAudit' => $v['settleAudit'],
            );
        }
        return $this->sendData('gx');
    }
    /**
     * 包装场租数据
     * @param array $rentData
     */
    function packRentData($rentListData = array()){
        //清除数据
        $this->clearData();
        foreach ($rentListData as $v){
            $this->data[] = array(
                'serialId'    => $v['id'],
                'dlrCode'   => $v['dlrCode'],
                'dlrName'   => $v['dlrName'],
                'procId'    => $v['procId'],
                'projectId' => $v['projectId'],
                'year'      => $v['year'],
                'month'     => $v['timeUnitStr'],
                'unitPrice' => $v['unitPrice'], 
                'areaSize' => $v['areaSize'],
                'budgetApply' => $v['budgetApply'],
                'storeNum' => $v['storeNum'],
                'oneStorePrice' => $v['oneStorePrice'],
                'budgetStoreTotal' => $v['budgetStoreTotal'],
                'budgetQiTotal' => $v['budgetQiTotal'],
                'budgetRmpSupport' => $v['budgetRmpSupport'],
                'budgetAuditStr' => $v['budgetAuditStr'],
                'settleFileUrl' => $v['fileUrl'],
                'settleApply' => $v['settleApply'],
                'settleStoreTotal' => $v['settleStoreTotal'],
                'settleQiTotal' => $v['settleQiTotal'],
                'settleRmpSupport' => $v['settleRmpSupport'],
                'settleAuditStr' => $v['settleAuditStr'],
            );
        }
        return $this->sendData('rent');
    }
    /**
     * 包装配套数据
     * @param array $rentData
     */
    function packMatchingData($matchingListData = array()){
        //清除数据
        $this->clearData();
        foreach ($matchingListData as $v){
            $this->data[] = array(
                'serialId'    => $v['id'],
                'dlrCode'   => $v['dlrCode'],
                'dlrName'   => $v['dlrName'],
                'procId'    => $v['procId'],
                'projectId' => $v['projectId'],
                'year'      => $v['year'],
                'month'     => $v['timeUnitStr'],
                'typeStr' => $v['typeStr'],
                'descTypeStr' => $v['descTypeStr'],
                'startTime' => $v['startTime'],
                'endTime' => $v['endTime'],
                'remarks' => $v['remarks'],
                'budgetApply' => $v['budgetApply'],
                'budgetAuditStr' => $v['budgetAuditStr'],
                'budgetAudit' => $v['budgetAudit'],
                'settleFileUrl' => $v['fileUrl'],
                'settleApply' => $v['settleApply'],
                'settleAuditStr' => $v['settleAuditStr'],
                'settleAudit' => $v['settleAudit'],                
            );
        }
        return $this->sendData('matching');
    }
    /**
     * 发送数据
     */
    function sendData($dataType = 'sum'){
        $accessToken = $this->getAccessToken();
        $urlParams = array(
            'mainCmd'   => $this->mainCmdList[$dataType],
            'accessToken'   => $accessToken,
        );
        $url = $this->apiUrl . $this->apiExtList[$dataType].'?'.httpBuildQuery($urlParams);
        $data = array(
            'params'       => $this->data,
        );
        $returnData = $this->httpGet($url, jsonEncode($data), array('Content-Type: application/json'));
        $jsonData = jsonDecode($returnData);
        //var_dump($jsonData);
        //exit();
        //提交成功
        if (isset($jsonData['authcHead']) && $jsonData['authcHead']['retnCode'] == 0){
            return true;
        }else{
            return false;
        }
    }
}